package java9_21.Test_6;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/*随机流：既可以输入又可以输出，既可以做源也可以做目的地*/
public class RandomAccessFileTest {
    public static void main(String[] args) {
        RandomAccessFile r=null;
        File f=new File("D:\\第一小组\\第一.txt");
        try {
           r=new RandomAccessFile(f,"ab");
           int []arr={1,2,3,4,5,6,7,8,9,10};
            for (int i = 0; i < arr.length; i++) {
                try {
                    r.writeInt(arr[i]);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            for (int i=arr.length;i>=0;i--){
                try {
                    r.seek(i*4);
                    System.out.println(","+r.readInt());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                r.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
